﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class city_s : System.Web.UI.Page
{
    int no;
    public string city_name;

    protected void Page_Load(object sender, EventArgs e)
    {
        GeoLingManager glManager = new GeoLingManager(Request, Response);
        String _lang = glManager.getLang();
        DatabaseUtil dbUtil = new DatabaseUtil();
        Hashtable resultTable = null;
        ArrayList al = null;
        //Get user's preferred langugage in int
        _lang = dbUtil.getLanguageNo(_lang).ToString();

        no = int.Parse(Request["no"]);

        ctpDataContext db = new ctpDataContext();
        ct_s_profile cts = db.ct_s_profiles.Single(p =>p.valid==true&& p.no == no && p.language_no == (int)Session["now_user_lang_no"]);

        this.Page.Title = "Citiport - " + cts.ct_profile.title.Trim().ToUpper() + " - " + cts.title.Trim() + " - travel, living, friendship";

        this.Label1.Text = cts.title;
        this.Label4.Text = cts.title;


        city_name = cts.ct_profile.title.Trim();
        this.HyperLink2.Text =city_name;
        this.HyperLink2.NavigateUrl = "city.aspx?no=" + cts.ct_no;


        var edit_history = from p in db.ct_s_profiles
                           where p.no == no && p.language_no == (int)Session["now_user_lang_no"]
                           orderby p.no
                           select p;

        if (edit_history.Count() == 0) this.Label16.Visible = false;

       // ct_s_profile first = db.ct_s_profiles.Single(p => p.ver == 0 && p.no == no);

       this.HyperLink1.Text =cts.user.nickname.Trim();
       this.HyperLink1.NavigateUrl = "user_profile.aspx?no=" + cts.create_user_no.ToString();
     
        this.DataList6.DataSource = edit_history;

        this.Label5.Text = cts.ranking.ToString();
        this.Label7.Text = cts.ranking_local.ToString();
        this.Label8.Text = cts.ranking_been_there.ToString();

        this.Label9.Text = cts.recommend_to_everyone.ToString();
        this.Label10.Text = cts.recommend_to_first.ToString();
        this.Label11.Text = cts.recommend_to_locals.ToString();


        if (cts.user.profile_picture != null)
        {
            this.Image2.ImageUrl = "user_imgs/1/" + cts.user.profile_picture.Trim();
        }
        else
        {
            this.Image2.Visible = false;
        }


        this.HyperLink3.NavigateUrl = "create_city_s.aspx?no1=" + cts.no;

        if (cts.main_pic_fn != null)
        {
            this.Image1.ImageUrl = "city_imgs/2/" + cts.main_pic_fn.Trim();
        }
        else
            this.Image1.Visible = false;


        if (cts.profile != null)
        {
            this.Label2.Text = cts.profile.ToString().Replace("\n", "<br>");
        }
        else
        {
            this.Label2.Text = "The profile of this item is not edited yet";
        }

        //bind nearby
        var nearitem = from p in db.ct_s_relationships
                       join q in db.ct_s_profiles on p.ct_s_no2 equals q.no
                       where p.type == 2 && p.ct_s_no1 == no && q.language_no == (int)Session["now_user_lang_no"] && q.valid==true
                       select new
                       {
                           no = p.ct_s_no2,
                           title = q.title
                       };

        this.DataList1.DataSource = nearitem;

        //bind inside item
        var insideitem = from p in db.ct_s_relationships
                       join q in db.ct_s_profiles on p.ct_s_no2 equals q.no
                       where p.type == 1 && p.ct_s_no1 == no && q.language_no == (int)Session["now_user_lang_no"] && q.valid==true
                       select new
                       {
                           no = p.ct_s_no2,
                           title = q.title
                       };

        this.DataList4.DataSource = insideitem;

        //bind pic
        var pic = from p in db.ct_s_pics
                  where p.ct_s_no == no
                  orderby p.addtime
                  select p;
        foreach (ct_s_pic p in pic)
        {
            p.filename = p.filename.Trim();
        }
        this.DataList2.DataSource = pic;

        //bin intro
        var intro = from p in db.ct_s_intros
                    where p.ct_s_no == no
                    orderby p.addtime
                    select p;
        
        al = new ArrayList();
        foreach (ct_s_intro p in intro)
        {
            p.intro = p.intro.Replace("\n", "<br>");

            if (p.user.profile_picture == null)
                p.user.profile_picture = "coming.gif";
            else
                p.user.profile_picture = p.user.profile_picture.Trim();
            al.Add(p.no);
        }
        resultTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.intro_intro, DataUtil.ObjsToInts(al.ToArray()));
        foreach (ct_s_intro p in intro)
        {
            p.intro = dbUtil.getPropertyPost(p.intro, resultTable, p.no, _lang);
        }
        this.DataList3.DataSource = intro;


        var myreview = from p in db.ct_s_reviews
                     where p.ct_s_no == no
                     select p;
        al = new ArrayList();
        foreach (ct_s_review r in myreview)
        {
            if (r.user.profile_picture != null)
            {
                r.user.profile_picture = r.user.profile_picture.Trim();
            }
            else
            {
                r.user.profile_picture = "coming.gif";
            }
            al.Add(r.no);
        }
        resultTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_review_comment, DataUtil.ObjsToInts(al.ToArray()));
        foreach (ct_s_review r in myreview)
        {
            r.comment = dbUtil.getPropertyPost(r.comment, resultTable, r.no, _lang);
        }
        this.DataList5.DataSource = myreview;


        //bind forum
        var foudata = from p in db.ct_s_forums
                   where p.ct_s_no == no && p.reply_to == 0
                   orderby p.last_post descending
                   select p;
        

        foreach (ct_s_forum f in foudata)
        {
            if (f.user.profile_picture != null)
            {
                f.user.profile_picture = f.user.profile_picture.Trim();
            }
            else
            {
                f.user.profile_picture = "coming.gif";
            }
        }

        if (foudata.Count() != 0)
        {
            this.HyperLink12.NavigateUrl = "city_s_forum.aspx?no=" + no.ToString();
        }
        else
        {
            this.HyperLink12.NavigateUrl = "city_s_forum_new.aspx?no=" + no.ToString();
            this.HyperLink12.Text = "Post new topic";

        }

        this.GridView1.DataSource =foudata.Take(5);
        this.DataBind();


        this.DataBind();

        this.HyperLink5.NavigateUrl = "city_s_upload_pic.aspx?no=" + no;
        this.HyperLink7.NavigateUrl = "city_s_new_intro.aspx?no=" + no;
        this.HyperLink9.NavigateUrl = "city_s_review.aspx?no=" + no;

        if (Session["userno"] != null)
        {
            this.Label15.Visible = false;

            if (cts.create_user_no == (int)Session["userno"])
            {
                this.LinkButton2.Visible = false;
            }
            else
            {
                this.LinkButton1.Visible = false;
                this.LinkButton2.Visible = false;

            }
        }
        else
        {
            this.LinkButton1.Visible = false;

        }
                     
                     

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        ctpDataContext db = new ctpDataContext();
        ct_s_profile cts = db.ct_s_profiles.Single(p => p.valid == true && p.no == no && p.language_no == (int)Session["now_user_lang_no"]&&p.create_user_no==(int)Session["userno"]);

        int ct_no = (int)cts.ct_no;
        cts.valid = false;

        db.SubmitChanges();
        Response.Redirect("city.aspx?no="+ct_no);

    
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "city_s.aspx?no=" + no);

    }
}
